package graphlib;

import dk.itu.KrakMap.MapPoint;

/**
 * Represents an Edge in a graph. Can be both directed an undirected
 * @author Peter Tiedemann petert@itu.dk
 *
 */
public class Edge<N extends Node> {
  public N v1;
  public N v2;
  private double weight;

  // direction of edge, see FORWARD,BACKWARD and BOTH below
  protected byte direction;

  // indicates the edge goes from v1 -> v2
  public static final byte FORWARD =1;

  // indicates the edge goes from v2 -> v1
  public static final byte BACKWARD =2;

  // indicates both of the above(undirected edge)
  public static final byte BOTH = FORWARD | BACKWARD;

  protected Edge() {
  }

  public Edge(N n1, N n2, byte direction){
    v1 = n1;
    v2 = n2;
    this.direction = direction;
  }

  /**
   * Returns the start node for a directed edge,
   * otherwise returns an arbitrary node in the edge
   */
  public N getStartNode(){
    if ((direction & FORWARD) != 0){
      return v1;
    }
    else return v2;
  }

  /**
   * Returns the node in the edge that is NOT the node n
   * @param n
   * @return
   */
  public N getOtherEndNode(N n){
    if (v1 == n)
      return v2;
    else
      return v1;
  }

    public N getOtherEndNode(int n){
    if (v1.getIndex() == n)
      return v2;
    else
      return v1;
  }

    public void setOtherEndNode(N n, N m){
        if (v1 == n)
      v2 = m;
    else
      v1 = m;
    }

  /**
   * Returns the end node for a directed edge,
   * otherwise returns an arbitrary node in the edge
   */
  public N getEndNode(){
    if ((direction & FORWARD) != 0)
      return v2;
    else
      return v1;
  }

    /**
     * @return the weight
     */
    public double getWeight()
    {
        return weight;
    }

    /**
     * @param weight the weight to set
     */
    public void setWeight(double weight)
    {
        this.weight = weight;
    }

    public MapPoint getStart() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public MapPoint getEnd() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
